package com.kgc.personconsumer.controller;

import com.kgc.basic.util.ResponseMsg;
import com.kgc.personcommon.pojo.Permission;
import com.kgc.personconsumer.feign.PermissionFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/perm")
@CrossOrigin
public class PermissionController {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private PermissionFeign permissionFeign;

    @RequestMapping("getPage")
    @ResponseBody
    public ResponseMsg getPage(@RequestParam String pageNow,@RequestParam String pageSize,@RequestParam(required = false) String name) {
        logger.info("getPage pageNow={} pageSize={} name={}", pageNow, pageSize, name);
        Map<String, Object> map = new HashMap<>();
        map.put("pageNow", Integer.parseInt(pageNow));
        map.put("pageSize", Integer.parseInt(pageSize));
        map.put("name", name);
        ResponseMsg responseMsg = permissionFeign.getPermPage(map);
        logger.debug("permissionFeign.getPermPage: {}", responseMsg+",map"+map);
        return responseMsg;
    }

    @PostMapping("add")
    @ResponseBody
    public ResponseMsg add(@RequestBody Permission permission) {
        logger.info("add permission: {}", permission);
        ResponseMsg responseMsg = permissionFeign.addPerm(permission);
        logger.debug("permissionFeign.addPerm: {}", responseMsg+",permission"+permission);
        return responseMsg;
    }

    @PostMapping("update")
    @ResponseBody
    public ResponseMsg update(@RequestBody Permission permission) {
        logger.info("update permission: {}", permission);
        ResponseMsg responseMsg = permissionFeign.updatePerm(permission);
        logger.debug("permissionFeign.updatePerm: {}", responseMsg+",permission"+permission);
        return responseMsg;
    }

    @PostMapping("del")
    @ResponseBody
    public ResponseMsg del(@RequestParam String id) {
        logger.info("del id={}", id);
        ResponseMsg responseMsg = permissionFeign.delPerm(id);
        logger.debug("permissionFeign.delPerm: {}", responseMsg+",id"+id);
        return responseMsg;
    }

    @RequestMapping("getById")
    @ResponseBody
    public ResponseMsg getById(@RequestParam String id) {
        logger.info("getById id={}", id);
        ResponseMsg responseMsg = permissionFeign.getPermById(id);
        logger.debug("permissionFeign.getPermById: {}", responseMsg+",id"+id);
        return responseMsg;
    }

    @RequestMapping("getAll")
    @ResponseBody
    public ResponseMsg getAll() {
        logger.info("getAll");
        Map<String, Object> map = new HashMap<>();
        ResponseMsg responseMsg = permissionFeign.getAllPerm(map);
        logger.debug("permissionFeign.getAllPerm: {}", responseMsg+",map"+map);
        return responseMsg;
    }
}
